package DMSXL._2023Before.ListNode_;

/*public class swapPairs_ {
    public static void main(String[] args) {
        ListNode head = new ListNode(3);
        ListNode node = new ListNode(1);
        node.next = head.next;
        head.next = node;
        Solution solution = new Solution();
        solution.swapPairs(head);

        while(head != null){
            System.out.println(head.val);
            head = head.next;
        }
    }
}
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode newhead = new ListNode();
        newhead.next = null;

        ListNode curhead = new ListNode();
        curhead.next = head;

        ListNode temp1 = new ListNode();
        ListNode temp2 = new ListNode();
        while(curhead.next != null){
            if(curhead.next.next == null){
                addnode(newhead,curhead.next);
                curhead.next = curhead.next.next;
            }
            else {
                System.out.println("there");
                temp1 = curhead.next;
                temp2 = curhead.next.next;
                addnode(newhead,temp2);
                addnode(newhead,temp1);
                curhead.next = temp2.next;
            }
        }
        return newhead.next;
    }
    public void addnode(ListNode head,ListNode node){
        ListNode newhead = head;
        ListNode temp = new ListNode();
        temp = node;
        while(newhead.next != null){
            newhead = newhead.next;
        }
        node.next = newhead.next;
        newhead.next = node;
    }
}*/